package com.lu.project.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.lu.project.exception.ErrorCode;
import com.lu.project.exception.ThrowUtils;
import com.lu.project.mapper.MessageTaskMapper;
import com.lu.project.model.dto.message.MessageTaskAddRequest;
import com.lu.project.model.dto.message.MessageTaskListRequest;
import com.lu.project.model.entity.BaseEntity;
import com.lu.project.model.entity.MessageTask;
import com.lu.project.model.entity.User;
import com.lu.project.model.vo.message.MessageTaskListVo;
import com.lu.project.service.MessageTaskService;

import com.lu.project.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author lu
* @description 针对表【message_task】的数据库操作Service实现
* @createDate 2025-02-28 19:58:07
*/
@Service
public class MessageTaskServiceImpl extends ServiceImpl<MessageTaskMapper, MessageTask>
    implements MessageTaskService{

    @Resource
    private UserService userService;
    @Override
    public void add(MessageTaskAddRequest messageTaskAddRequest) {
        MessageTask messageTask = BeanUtil.copyProperties(messageTaskAddRequest, MessageTask.class);
        LocalDateTime start = ObjectUtil.isEmpty(messageTaskAddRequest.getStartTime()) ? null : LocalDateTimeUtil.of(messageTaskAddRequest.getStartTime());
        LocalDateTime end = ObjectUtil.isEmpty(messageTaskAddRequest.getEndTime()) ? null : LocalDateTimeUtil.of(messageTaskAddRequest.getEndTime());
        messageTask.setStartTime(start);
        messageTask.setEndTime(end);
        check(messageTask);
        this.save(messageTask);

    }

    @Override
    public void updateMeg(MessageTaskAddRequest messageTaskAddRequest) {
        MessageTask messageTask = BeanUtil.copyProperties(messageTaskAddRequest, MessageTask.class);
        LocalDateTime start = ObjectUtil.isEmpty(messageTaskAddRequest.getStartTime()) ? null : LocalDateTimeUtil.of(messageTaskAddRequest.getStartTime());
        LocalDateTime end = ObjectUtil.isEmpty(messageTaskAddRequest.getEndTime()) ? null : LocalDateTimeUtil.of(messageTaskAddRequest.getEndTime());
        messageTask.setStartTime(start);
        messageTask.setEndTime(end);
        check(messageTask);
        this.updateById(messageTask);
    }

    @Override
    public Page<MessageTaskListVo> queryMsg(MessageTaskListRequest messageTaskListRequest) {

            QueryWrapper<MessageTask> msgQuery = new QueryWrapper<>();
            Long initiator = messageTaskListRequest.getInitiator();
            Long elderId = messageTaskListRequest.getElderId();
            LocalDateTime startTime = ObjectUtil.isEmpty(messageTaskListRequest.getStartTime()) ? null : LocalDateTimeUtil.of(messageTaskListRequest.getStartTime());
            LocalDateTime endTime = ObjectUtil.isEmpty(messageTaskListRequest.getEndTime()) ? null : LocalDateTimeUtil.of(messageTaskListRequest.getEndTime());
            long current = messageTaskListRequest.getCurrent();
            long pageSize = messageTaskListRequest.getPageSize();
            String sortField = messageTaskListRequest.getSortField();
            String sortOrder = messageTaskListRequest.getSortOrder();


            msgQuery.eq(ObjectUtil.isNotNull(initiator),"initiator",initiator);
            msgQuery.eq(ObjectUtil.isNotEmpty(elderId),"elder_id",elderId);
            msgQuery.ge(ObjectUtil.isNotEmpty(startTime),"start_time",startTime);
            msgQuery.le(ObjectUtil.isNotEmpty(endTime),"end_time",endTime);
            msgQuery.orderBy(StrUtil.isNotEmpty(sortField), sortOrder.equals("ascend"), sortField);

            Page<MessageTask> msPage = page(new Page<>(current, pageSize), msgQuery);

        List<MessageTaskListVo> collect = msPage.getRecords().stream().map(
                (record)->{
                    return BeanUtil.copyProperties(record, MessageTaskListVo.class);
                }
                )
                .filter((record)->{
                    return record.getEndTime().isAfter(LocalDateTime.now());
                })
                .toList();
        Page<MessageTaskListVo> messageTaskListRequestPage = new Page<>(msPage.getCurrent(),msPage.getPages(),msPage.getTotal());
        messageTaskListRequestPage.setRecords(collect);
        return messageTaskListRequestPage;

    }


    public void check(MessageTask messageTask){
        Long initiator = messageTask.getInitiator();
        Long elderId = messageTask.getElderId();
        String content = messageTask.getContent();
        LocalDateTime startTime = messageTask.getStartTime();
        LocalDateTime endTime = messageTask.getEndTime();

        User user = userService.getBaseMapper().selectById(elderId);
        ThrowUtils.throwIf(user==null,ErrorCode.PARAMS_ERROR,"目标老人信息不存在");
        ThrowUtils.throwIf(StrUtil.isBlank(content), ErrorCode.PARAMS_ERROR,"待办内容为空");
        ThrowUtils.throwIf(elderId==null, ErrorCode.PARAMS_ERROR,"目标老人为空");
        ThrowUtils.throwIf(startTime==null, ErrorCode.PARAMS_ERROR,"开始时间为空");
        ThrowUtils.throwIf(endTime==null, ErrorCode.PARAMS_ERROR,"结束时间为空");
        ThrowUtils.throwIf(startTime.isAfter(endTime),ErrorCode.PARAMS_ERROR,"待办结束时间不能小于开始时间");
        ThrowUtils.throwIf(content.length()>300,ErrorCode.PARAMS_ERROR,"待办内容太长");
    }
}




